import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';
import { unref } from 'vue';

const { createConfirm } = useMessage();

export enum Api {
  list = '/cellAddress/list',
  listByCommunity = 'cellAddress/listByCommunity',
  edit = '/cellAddress/edit',
  save = '/cellAddress/add',
  delete = '/cellAddress/delete',
  deleteBatch = '/cellAddress/deleteBatch',
  exportXls = '/cellAddress/exportXls',
  queryByCommunity = '/cellAddress/queryByCommunity',
  apiBaiduList = '/cellAddress/getInputList',
}

export const list = (params) => defHttp.get({ url: Api.list, params });

export const listByCommunity = () => defHttp.get({ url: Api.queryByCommunity });
export const apiBaiduList = (keyword, address) => defHttp.get({ url: Api.apiBaiduList, params: { query: keyword, region: address } });
/**
 * 保存或者更改消息模板
 */
export const saveOrUpdate = (params, isUpdate) => {
  if (unref(isUpdate)) {
    return defHttp.put({ url: Api.edit, params });
  } else {
    return defHttp.post({ url: Api.save, params });
  }
};
/**
 * 批量退单
 * @param params
 * @param confirm
 */
export const deleteBatch = (params, confirm = false) => {
  return new Promise((resolve, reject) => {
    const doDelete = () => {
      resolve(defHttp.delete({ url: Api.deleteBatch, params }, { joinParamsToUrl: true }));
    };
    if (confirm) {
      createConfirm({
        iconType: 'warning',
        title: '退单',
        content: '确定要退单吗？',
        onOk: () => doDelete(),
        onCancel: () => reject(),
      });
    } else {
      doDelete();
    }
  });
};
